﻿@model PostEditViewModel
@{
    if (Model.PostId <= 0)
    {
        ViewBag.Title = this.GetTtile("New Post");
    }
    else
    {
        ViewBag.Title = this.GetTtile("Editando: \"" + Model.Title + "\"");
    }
}
@section head{
    <script type="text/javascript" src="@this.Static("~/Scripts/jquery.validate.min.js")"></script>
    <script type="text/javascript" src="@this.Static("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>
    <script type="text/javascript" src="@this.Static("~/Scripts/ckeditor/ckeditor.js")"></script>
    <script type="text/javascript" src="@this.Static("~/Scripts/ckeditor/adapters/jquery.js")"></script>
}
<article id="new-post">
    @using (Html.BeginForm())
    {
        <fieldset>
            <legend>Post</legend>
            @Html.ValidationSummary(true)
            @Html.HiddenFor(model => model.PostId)
            <div class="editor-label">
                @Html.LabelFor(model => model.Title)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Title)
                @Html.ValidationMessageFor(model => model.Title)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.Text)
            </div>
            <div>
                You have to insert a "line break" to separate summary of post.</div>
            <div class="editor-field">
                @Html.ValidationMessageFor(model => model.Text)
                @Html.EditorFor(model => model.Text)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.Tags)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Tags)
                @Html.ValidationMessageFor(model => model.Tags)
            </div>
            <p>
                <input type="submit" value="Save" />
            </p>
        </fieldset>
    }
    <div>
        @if (Model.PostId != 0)
        {
            @Html.ActionLink("Back!", "Index", new { area = string.Empty, controller = "Post", name = Model.EncodedTitle })
        }
        else
        {
            <a href="/">Back!</a>         
        }
    </div>
</article>
<script type="text/javascript">
    $(document).ready(function () {
        var textName = "Text";
        //Init editor
        CKEDITOR.replace(textName,
        {
            language: '@this.UICulture.Substring(0, 2)'
        });
        var editorCke = CKEDITOR.instances[textName];
        //Set ignore to empty. the field to validate is hidden
        $(editorCke.element.$).closest('form').validate().settings.ignore = "";
        //When editor lose focus
        editorCke.on('blur', function (e) {
            //Update element and the form validation
            e.editor.updateElement();
            $(e.editor.element.$).valid();
        });
    });
</script>
